package business

import (
	"context"
	"encoding/json"
	"github.com/streadway/amqp"
)

type FailedHandleQueue struct {
	Implementation
}

func (failedHandle *FailedHandleQueue) Init() {
	//初始化重试脚本
	failedHandle.Implementation.Init()
}

/**
 * 配置文件名称
 */
func (failedHandle *FailedHandleQueue) GetConfigKey() string {
	return "golang-manage-admin.failed.handle"
}

/**
 * 队列消费
 */
func (failedHandle *FailedHandleQueue) Delivery(ctx context.Context, msg amqp.Delivery) {
	data := new(QueueMsgBaseDataForm)
	responseMode := ResponseMode{
		ResponseType: "ack",
	}
	defer func() {
		failedHandle.Implementation.ReplyQueueHandler(responseMode, msg)
	}()

	// @Todo 消息解析
	if err := json.Unmarshal(msg.Body, data); err != nil {
		if string(msg.Body) != "" {
			_ = failedHandle.logger.Errorf("延时队列--消息解析失败:%s---%s", err.Error(), string(msg.Body))
		}
		return
	}

	//消息处理接收解析DATA
	failedHandle.logger.Debugf("失败队列--事件处理接收解析DATA:%+v", data)

	//发送消息到
	sErr := failedHandle.dealWithMsg(data, msg)
	if sErr != nil {
		_ = failedHandle.logger.Errorf("失败队列--消息处理失败:%+v", sErr.Error())
		return
	}

	failedHandle.logger.Debugf("消息处理成功DATA:%+v", data)

	return
}

func (failedHandle *FailedHandleQueue) dealWithMsg(data *QueueMsgBaseDataForm, msg amqp.Delivery) error {
	// @Todo 队列操作失败后操作（记录日志）
	_ = failedHandle.logger.Error("失败队列--事件处理接收解析DATA:%+v", data)
	return nil
}
